<template>
  <el-container class="layout-container">
    <el-aside width="260px" class="sidebar">
      <div class="logo">
        <div class="logo-icon">
          <el-icon size="24"><School /></el-icon>
        </div>
        <div class="logo-text">
          <h3>学生宿舍管理系统</h3>
        </div>
      </div>
      
      <div class="menu-container">
        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          router
          :collapse="false"
        >
          <el-menu-item index="/admin/dashboard" class="menu-item-custom">
            <div class="menu-item-content">
              <div class="menu-icon">
                <el-icon><DataBoard /></el-icon>
              </div>
              <span class="menu-text">控制台</span>
            </div>
          </el-menu-item>
          
          <el-menu-item index="/admin/users" class="menu-item-custom">
            <div class="menu-item-content">
              <div class="menu-icon">
                <el-icon><UserFilled /></el-icon>
              </div>
              <span class="menu-text">用户管理</span>
            </div>
          </el-menu-item>
          
          <el-menu-item index="/admin/dormitories" class="menu-item-custom">
            <div class="menu-item-content">
              <div class="menu-icon">
                <el-icon><OfficeBuilding /></el-icon>
              </div>
              <span class="menu-text">宿舍楼管理</span>
            </div>
          </el-menu-item>
          
          <el-menu-item index="/admin/rules" class="menu-item-custom">
            <div class="menu-item-content">
              <div class="menu-icon">
                <el-icon><Setting /></el-icon>
              </div>
              <span class="menu-text">选宿规则管理</span>
            </div>
          </el-menu-item>
          
          <el-menu-item index="/admin/announcements" class="menu-item-custom">
            <div class="menu-item-content">
              <div class="menu-icon">
                <el-icon><Bell /></el-icon>
              </div>
              <span class="menu-text">公告板管理</span>
            </div>
          </el-menu-item>
          
          <el-menu-item index="/admin/orders" class="menu-item-custom">
            <div class="menu-item-content">
              <div class="menu-icon">
                <el-icon><Money /></el-icon>
              </div>
              <span class="menu-text">费用订单管理</span>
            </div>
          </el-menu-item>
          
          <el-menu-item index="/admin/questionnaire-reports" class="menu-item-custom">
            <div class="menu-item-content">
              <div class="menu-icon">
                <el-icon><PieChart /></el-icon>
              </div>
              <span class="menu-text">问卷统计报表</span>
            </div>
          </el-menu-item>
          
          <el-menu-item index="/admin/repair-management" class="menu-item-custom">
            <div class="menu-item-content">
              <div class="menu-icon">
                <el-icon><Tools /></el-icon>
              </div>
              <span class="menu-text">报修管理</span>
            </div>
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 底部用户信息 -->
      <div class="sidebar-footer">
        <div class="user-card">
          <div class="user-avatar">
            <el-icon><Avatar /></el-icon>
          </div>
          <div class="user-info-text">
            <div class="username">{{ userStore.userInfo.username }}</div>
            <div class="user-role">系统管理员</div>
          </div>
          <el-dropdown @command="handleCommand" placement="top-start">
            <div class="user-actions">
              <el-icon><MoreFilled /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="logout" divided>
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-aside>
    
    <el-container>
      <el-header class="header">
        <div class="header-left">
          <div class="breadcrumb-container">
            <el-icon class="breadcrumb-icon"><Location /></el-icon>
            <span class="breadcrumb">{{ currentPageTitle }}</span>
          </div>
        </div>
        
        <div class="header-right">
          <div class="header-actions">
            <el-tooltip content="通知" placement="bottom">
              <div class="action-item">
                <el-badge :value="3" class="notification-badge">
                  <el-icon size="18"><Bell /></el-icon>
                </el-badge>
              </div>
            </el-tooltip>
            
            <el-tooltip content="设置" placement="bottom">
              <div class="action-item">
                <el-icon size="18"><Setting /></el-icon>
              </div>
            </el-tooltip>
          </div>
        </div>
      </el-header>
      
      <el-main class="main-content">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useUserStore } from '../stores/user'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

const activeMenu = computed(() => route.path)

const currentPageTitle = computed(() => {
  const titleMap = {
    '/admin/dashboard': '控制台',
    '/admin/users': '用户管理',
    '/admin/dormitories': '宿舍楼管理',
    '/admin/rules': '选宿规则管理',
    '/admin/announcements': '公告板管理',
    '/admin/orders': '费用订单管理',
    '/admin/questionnaire-reports': '问卷统计报表',
    '/admin/repair-management': '报修管理'
  }
  return titleMap[route.path] || '管理员系统'
})

const handleCommand = async (command) => {
  if (command === 'logout') {
    try {
      await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      
      await userStore.logout()
      ElMessage.success('退出登录成功')
      router.push('/login')
    } catch (error) {
      // 用户取消操作
    }
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.sidebar {
  background: #2c3e50;
  overflow: hidden;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  position: relative;
}

.logo {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  padding: 0 20px;
  background: #34495e;
  color: white;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.logo-icon {
  margin-right: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: #409EFF;
  border-radius: 6px;
  color: white;
}

.logo-text h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: white;
}

.menu-container {
  padding: 16px 0;
  height: calc(100vh - 140px);
  overflow-y: auto;
}

.menu-container::-webkit-scrollbar {
  width: 4px;
}

.menu-container::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.1);
}

.menu-container::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.3);
  border-radius: 2px;
}

.sidebar-menu {
  border: none;
  background: transparent !important;
  padding: 0 16px;
}

.menu-item-custom {
  margin-bottom: 4px;
  border-radius: 8px;
  overflow: hidden;
  background: transparent !important;
  border: none !important;
  transition: all 0.2s ease;
  position: relative;
}

.menu-item-custom:hover {
  background: rgba(255, 255, 255, 0.08) !important;
}

.menu-item-custom.is-active {
  background: #409EFF !important;
}

.menu-item-custom.is-active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 20px;
  background: white;
  border-radius: 0 2px 2px 0;
}

.menu-item-content {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  width: 100%;
}

.menu-icon {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  color: rgba(255, 255, 255, 0.8);
  font-size: 16px;
  transition: color 0.2s ease;
}

.menu-item-custom:hover .menu-icon {
  color: white;
}

.menu-item-custom.is-active .menu-icon {
  color: white;
}

.menu-text {
  flex: 1;
  color: rgba(255, 255, 255, 0.8) !important;
  font-size: 14px;
  font-weight: 500;
  transition: color 0.2s ease;
}

.menu-item-custom:hover .menu-text {
  color: white !important;
}

.menu-item-custom.is-active .menu-text {
  color: white !important;
  font-weight: 600;
}

.sidebar-footer {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 16px;
  background: #34495e;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.user-card {
  display: flex;
  align-items: center;
  padding: 12px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 8px;
  transition: all 0.2s ease;
}

.user-card:hover {
  background: rgba(255, 255, 255, 0.12);
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  background: #409EFF;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  margin-right: 12px;
  font-size: 16px;
}

.user-info-text {
  flex: 1;
}

.username {
  color: white;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.2;
}

.user-role {
  color: rgba(255, 255, 255, 0.7);
  font-size: 12px;
  line-height: 1;
  margin-top: 2px;
}

.user-actions {
  width: 28px;
  height: 28px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(255, 255, 255, 0.7);
  cursor: pointer;
  transition: all 0.2s ease;
}

.user-actions:hover {
  background: rgba(255, 255, 255, 0.1);
  color: white;
}

.header {
  background: white;
  border-bottom: 1px solid #e4e7ed;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
}

.breadcrumb-container {
  display: flex;
  align-items: center;
}

.breadcrumb-icon {
  margin-right: 8px;
  color: #409EFF;
  font-size: 16px;
}

.header-left .breadcrumb {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.action-item {
  width: 36px;
  height: 36px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  color: #606266;
  background: #f5f7fa;
}

.action-item:hover {
  background: #409EFF;
  color: white;
}

.notification-badge {
  position: relative;
}

.main-content {
  background: #f5f7fa;
  padding: 20px;
  overflow-y: auto;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    width: 60px !important;
  }
  
  .logo-text {
    display: none;
  }
  
  .menu-text {
    display: none;
  }
  
  .user-info-text {
    display: none;
  }
}
</style> 